home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / crssrc16.zoo / src / tputs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  5.3 KB  |  229 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tputs     output string with appropriate padding
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap
  28.  *
  29.  *  SYNOPSIS
  30.  *
  31.  *    tputs(cp,affcnt,outc)
  32.  *    char *cp;
  33.  *    int affcnt;
  34.  *    int (*outc)();
  35.  *
  36.  *  DESCRIPTION
  37.  *
  38.  *    Outputs string pointed to by cp, using function outc, and
  39.  *    following it with the appropriate number of padding characters.
  40.  *    Affcnt contains the number of lines affected, which is used
  41.  *    as a multiplier for the specified per line pad time.  If
  42.  *    per line pad count is not applicable, affcnt should be 1,
  43.  *    NOT zero.
  44.  *
  45.  *    The format of the string pointed to by cp is:
  46.  *
  47.  *        [pad time][*]<string to send>
  48.  *
  49.  *        where:    pad time => time to delay in milliseconds
  50.  *            * => specifies that time is per line
  51.  *            
  52.  *    The pad character is assumed to reside in the external
  53.  *    variable "PC".  Also, the external variable "ospeed"
  54.  *    should contain the output speed of the terminal as
  55.  *    encoded in /usr/include/sgtty.h  (B0-B9600).
  56.  *
  57.  *  BUGS
  58.  *
  59.  *    Digit conversion is based on native character set
  60.  *    being ASCII.
  61.  *
  62.  */
  63.  
  64. /*
  65.  *    Miscellaneous stuff
  66.  */
  67.  
  68. #include <stdio.h>
  69. #include <ctype.h>
  70. #include <termcap.h>
  71.  
  72. #ifndef _COMPILER_H
  73. #  include <compiler.h>
  74. #endif
  75.  
  76. # ifdef SGTTY
  77. extern char PC;            /* Pad character to use */
  78. extern char ospeed;        /* Encoding of output speed */
  79.  
  80. static int _times[] = {
  81.     0,                /* Tenths of ms per char 0 baud */
  82.     2000,            /* Tenths of ms per char 50 baud */
  83.     1333,            /* Tenths of ms per char 75 baud */
  84.     909,            /* Tenths of ms per char 110 baud */
  85.     743,            /* Tenths of ms per char 134 baud */
  86.     666,            /* Tenths of ms per char 150 baud */
  87.     500,            /* Tenths of ms per char 200 baud */
  88.     333,            /* Tenths of ms per char 300 baud */
  89.     166,            /* Tenths of ms per char 600 baud */
  90.     83,                /* Tenths of ms per char 1200 baud */
  91.     55,                /* Tenths of ms per char 1800 baud */
  92.     41,                /* Tenths of ms per char 2400 baud */
  93.     20,                /* Tenths of ms per char 4800 baud */
  94.     10                /* Tenths of ms per char 9600 baud */
  95. };
  96. # endif
  97.  
  98. # ifdef SGTTY
  99. static void do_padding __PROTO((int ptime, int (*outc )(int)));
  100. # endif
  101.  
  102.  
  103. /*
  104.  *  PSEUDO CODE
  105.  *
  106.  *    Begin tgoto
  107.  *        If string pointer is invalid then
  108.  *        Return without doing anything.
  109.  *        Else
  110.  *        For each pad digit (if any)
  111.  *            Do decimal left shift.
  112.  *            Accumulate the lower digit.
  113.  *        End for
  114.  *        Adjust scale to tenths of milliseconds
  115.  *        If there is a fractional field
  116.  *            Skip the decimal point.
  117.  *            If there is a valid tenths digit
  118.  *            Accumulate the tenths.
  119.  *            End if
  120.  *            Discard remaining digits.
  121.  *        End if
  122.  *        If per line is specified then
  123.  *            Adjust the pad time.
  124.  *            Discard the per line flag char.
  125.  *        End if
  126.  *        While there are any characters left
  127.  *            Send them out via output function.
  128.  *        End while
  129.  *        Transmit any padding required.
  130.  *        End if
  131.  *    End tgoto
  132.  *
  133.  */
  134.  
  135. void tputs(cp,affcnt,outc)
  136. char *cp;
  137. int affcnt;
  138. int (*outc) __PROTO((int));
  139. {
  140.     int ptime;            /* Pad time in tenths of milliseconds */
  141.  
  142.     if (cp == NULL || *cp == '\0') {
  143.     return;
  144.     } else {
  145.     for (ptime = 0; isdigit(*cp); cp++) {
  146.         ptime *= 10;
  147.         ptime += (*cp - '0');
  148.     }
  149.     if (ptime)
  150.         ptime *= 10;
  151.     if (*cp == '.') {
  152.         cp++;
  153.         if (isdigit(*cp)) {
  154.         ptime += (*cp++ - '0');
  155.         }
  156.         while (isdigit(*cp)) {cp++;}
  157.     }
  158.     if (*cp == '*') {
  159.         ptime *= affcnt;
  160.         cp++;
  161.     }
  162.     while (*cp != '\0') {
  163.         (*outc)(*cp++);
  164.     }
  165. # ifdef SGTTY
  166.     do_padding(ptime,outc);
  167. # endif
  168.     }
  169. }
  170.  
  171. # ifdef SGTTY
  172. /*
  173.  *  FUNCTION
  174.  *
  175.  *    do_padding    transmit any pad characters required
  176.  *
  177.  *  SYNOPSIS
  178.  *
  179.  *    static do_padding(ptime,outc)
  180.  *    int ptime;
  181.  *    int (*outc)();
  182.  *
  183.  *  DESCRIPTION
  184.  *
  185.  *    Does any padding required as specified by ptime (in tenths
  186.  *    of milliseconds), the output speed given in the external
  187.  *    variable ospeed, and the pad character given in the
  188.  *    external variable PC.
  189.  *
  190.  */
  191.  
  192. /*
  193.  *  PSEUDO CODE
  194.  *
  195.  *    Begin do_padding
  196.  *        If there is a non-zero pad time then
  197.  *        If the external speed is in range then
  198.  *            Look up the delay per pad character.
  199.  *            Round pad time up by half a character.
  200.  *            Compute number of characters to send.
  201.  *            For each pad character to send
  202.  *            Transmit the pad character.
  203.  *            End for
  204.  *        End if
  205.  *        End if
  206.  *    End do_padding
  207.  *
  208.  */
  209.  
  210. static void do_padding(ptime,outc)
  211. int ptime;
  212. int (*outc) __PROTO((int));
  213. {
  214.     register int nchars;
  215.     register int tpc;
  216.  
  217.     if (ptime != 0) {
  218.     if (ospeed >= 0 && ospeed <= (sizeof(_times)/ sizeof(int))) {
  219.         tpc = _times[ospeed];
  220.         ptime += (tpc / 2);
  221.         nchars = ptime / tpc;
  222.         for ( ; nchars > 0; --nchars) {
  223.         (*outc)(PC);
  224.         }
  225.     }
  226.     }
  227. }
  228. # endif
  229.